﻿using System;
using System.Collections.Generic;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// json元素
    /// </summary>
    public class JElement
    {
        private readonly JsonOriginValueTypes _originValueType;
        /// <summary>
        /// json字符串中推测出原始值类型
        /// </summary>
        public JsonOriginValueTypes OriginValueType
        {
            get { return _originValueType; }
        }

        private readonly string _name = null;
        /// <summary>
        /// 项名称
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        private readonly string _value = null;
        /// <summary>
        /// 值
        /// </summary>
        public string Value
        {
            get { return _value; }
        }

        private readonly JElementCollection _iList = null;
        /// <summary>
        /// Ilist子元素集合
        /// </summary>
        internal JElementCollection IList
        {
            get { return _iList; }
        }



        private readonly Dictionary<string, JElement> _objectDic = null;
        /// <summary>
        /// 对象子元素集合
        /// </summary>
#if NET4_0
        internal Dictionary<string, JElement> ObjectDic
#else
		internal IReadOnlyDictionary<string, JElement> ObjectDic
#endif        
		{
            get { return _objectDic; }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="originValueType"></param>
        /// <param name="value"></param>
        public JElement(JsonOriginValueTypes originValueType, string value)
        {
            this._value = value;
            this._originValueType = originValueType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="originValueType"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public JElement(JsonOriginValueTypes originValueType, string name, string value)
        {
            this._originValueType = originValueType;
            this._name = name;
            this._value = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="childrenDic"></param>
        public JElement(Dictionary<string, JElement> childrenDic)
        {
            this._originValueType = JsonOriginValueTypes.Object;
            this._objectDic = childrenDic;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="children"></param>
        public JElement(JElementCollection children)
        {
            this._originValueType = JsonOriginValueTypes.IList;
            this._iList = children;
        }




        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns>String</returns>
        public override string ToString()
        {
            string value;
            switch (this._originValueType)
            {
                case JsonOriginValueTypes.Object:
                    value = this._objectDic.Count.ToString();
                    break;
                case JsonOriginValueTypes.IList:
                    value = this._iList.Count.ToString();
                    break;
                default:
                    value = this._value;
                    break;
            }

            return $"{this._name}:{value} {this._originValueType}";
        }
    }
}
